package com.summerbird.mallchat.common.common.utils.discover;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.jsoup.nodes.Document;

import java.util.ArrayList;
import java.util.List;


public class PrioritizedUrlDiscover extends AbstractUrlDiscover{

    private final List<UrlDiscover> urlDiscovers = new ArrayList<>(2);

    public PrioritizedUrlDiscover(){
        this.urlDiscovers.add(new WxUrlDiscover());
        this.urlDiscovers.add(new CommonUrlDiscover());
    }

    @Nullable
    @Override
    public String getTitle(Document document) {
        for (UrlDiscover discover : urlDiscovers) {
            String title = discover.getTitle(document);
            if(StringUtils.isNotBlank(title)){
                return title;
            }
        }
        return null;
    }

    @Nullable
    @Override
    public String getDescription(Document document) {
        for (UrlDiscover discover : urlDiscovers) {
            String description = discover.getDescription(document);
            if(StringUtils.isNotBlank(description)){
                return description;
            }
        }
        return null;
    }

    @Nullable
    @Override
    public String getImage(String url, Document document) {
        for (UrlDiscover discover : urlDiscovers) {
            String image = discover.getImage(url, document);
            if(StringUtils.isNotBlank(image)){
                return image;
            }
        }
        return null;
    }
}
